package cn.com.service.impl;

import static cn.com.support.message.MessageCodeEnum.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.com.commons.Paginator;
import cn.com.commons.utils.NumberUtil;
import cn.com.dal.dao.VerifyDAO;
import cn.com.dal.dataobject.VerifyDO;
import cn.com.service.VerifyService;
import cn.com.support.dal.DAOException;
import cn.com.support.message.MessageResult;

/**
 * 预核算提交，分院报销SERVICE实现类
 * 
 * STATUS: -1 审核驳回
 *          0 审核中
 *          2 审核通过
 *         -2 审核取消(发起人取消)
 * @author Zoe
 */
@Service
public class VerifyServiceImpl implements VerifyService {

	private Logger logger = LoggerFactory.getLogger(VerifyServiceImpl.class);

	@Autowired
	private VerifyDAO<VerifyDO> verifyDAO;

	@Override
	public <C> Paginator<C, List<VerifyDO>> selectByPage(
			Paginator<C, List<VerifyDO>> paginator) {
		Paginator<C, List<VerifyDO>> result = null;
		try {
			result = verifyDAO.selectForPage(paginator);
		} catch (Exception e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]",
							paginator.getCondition()), e);
		}
		return result;
	}

	@Override
	public MessageResult<Long> insert(VerifyDO projectDO) {
		MessageResult<Long> result = new MessageResult<Long>(false, null,
				ERROR_INSERT);
		try {
			Long id = verifyDAO.insert(projectDO);
			if (id != null && id > 0) {
				result = new MessageResult<Long>(true, id, INFO_SUCCEED);
			}
		} catch (Exception e) {
			logger.error(
					String.format(ERROR_INSERT.descript() + ":[%s]", projectDO),
					e);
		}
		return result;
	}

	@Transactional
	@Override
	public MessageResult<Integer> updateById(VerifyDO projectDO) {
		MessageResult<Integer> result = new MessageResult<Integer>(false, null,
				ERROR_UPDATE);
		try {
			Integer id = verifyDAO.updateById(projectDO);
			if (id != null && id > 0) {
				result = new MessageResult<Integer>(true, id, INFO_SUCCEED);
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			logger.error(
					String.format(ERROR_UPDATE.descript() + ":[%s]", projectDO),
					e);
		}
		return result;
	}

	@Transactional
	@Override
	public MessageResult<Long> deleteById(Long[] ids) {
		MessageResult<Long> result = new MessageResult<Long>(false, null,
				ERROR_DELETE);
		Integer count= null;
		for (int i = 0; i < ids.length; i++) {
			try {
				count=verifyDAO.deleteById(ids[i]);
				if (NumberUtil.isPositive(count)) {
					result = new MessageResult<Long>(true, ids[i], INFO_SUCCEED);
					continue;
				} else {
					// 删除失败，事物回滚
					result = new MessageResult<Long>(false, ids[i],
							ERROR_DELETE);
					TransactionAspectSupport.currentTransactionStatus()
							.setRollbackOnly();
					return result;
				}
			} catch (Exception e) {
				// 删除失败，事物回滚
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				result = new MessageResult<Long>(false, ids[i], ERROR_DELETE);
				logger.error(
						String.format(ERROR_DELETE.descript() + ":[%d]", i), e);
			}
		}
		return result;
	}

	@Override
	public List<VerifyDO> selectList(Map<Object, Object> params) {
		List<VerifyDO> result = null;
		try {
			result = verifyDAO.selectList(params);
		} catch (Exception e) {
			logger.error(String.format(ERROR_SELECT.descript() + ":[%s]"), e);
		}
		return result;
	}

	@Override
	public MessageResult<VerifyDO> selectById(Long id) {
		MessageResult<VerifyDO> result = new MessageResult<VerifyDO>(false, null,
				ERROR_SELECT);
		VerifyDO projectDO = null;
		try {
			projectDO = verifyDAO.selectById(id);
			result.setMessageResult(true, projectDO, INFO_SUCCEED);
		} catch (DAOException e) {
			logger.error(String.format(ERROR_SELECT.descript() + ":[%d]", id),
					e);
			result.setMessageResult(false, projectDO, ERROR_SELECT);
		}
		return result;
	}

	@Override
	public Long selectNeedVerifyTotalCount(String userName) {
		try {
			return verifyDAO.selectNeedVerifyTotalCount(userName);
		} catch (DAOException e) {
			logger.error(String.format(ERROR_SELECT.descript() + ":[%s]", userName),
					e);
		}
		return 0L;
	}

	@Override
	public Long selectVerifingTotalCount(HashMap<Object, Object> params) {
		try {
			return verifyDAO.selectVerifingTotalCount(params);
		} catch (DAOException e) {
			logger.error(String.format(ERROR_SELECT.descript() + ":[%s]", params),
					e);
		}
		return 0L;
	}

}
